/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.raflik.queryxml.ui.model;

import java.util.ArrayList;
import java.util.List;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;
import org.openide.util.NbBundle;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import pl.raflik.queryxml.lib.ResultType;
import pl.raflik.queryxml.lib.XMLNodePresenter;

/**
 *
 * @author raflik
 */
public class ResultTableModel implements TableModel {

    public enum Columns {
        TYPE,
        NAME,
        VALUE;

        public String toText() {
            return NbBundle.getMessage(ResultRowModel.Columns.class, "ResultRowModel.Columns." + this);
        }
    }

    private final List<Object> listModel = new ArrayList<Object>();

    public ResultTableModel() {
    }

    public ResultTableModel(Object obj) {
        this();
        if (obj instanceof NodeList) {
            NodeList nodeList = (NodeList) obj;
            int length = nodeList.getLength();
            for (int i = 0; i < length; i++) {
                Node node = nodeList.item(i);
                listModel.add(node);
            }
        } else if (obj instanceof List) {
            List<?> list = (List) obj;
            listModel.addAll(list);
        } else if (obj != null) {
            listModel.add(obj);
        }
    }

    public int getIndexOfChild(Object parent, Object child) {
        return listModel.indexOf(child);
    }

    @Override
    public int getRowCount() {
        return listModel.size();
    }

    @Override
    public int getColumnCount() {
        return Columns.values().length;
    }

    @Override
    public String getColumnName(int columnIndex) {
        return Columns.values()[columnIndex].toText();
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return String.class;
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return false;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Object obj = listModel.get(rowIndex);
        switch (columnIndex) {
            case 0:
                return ResultType.formatType(obj.getClass());
            case 1:
                if (obj instanceof Node) {
                    Node node = (Node) obj;
                    return XMLNodePresenter.getNodeName(node);
                }
                return obj.getClass().getCanonicalName();
            case 2:
                if (obj instanceof Node) {
                    Node node = (Node) obj;
                    return XMLNodePresenter.getNodeValue(node);
                }
                return obj;
            default:
                assert false;
        }
        return null;

    }

    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void addTableModelListener(TableModelListener l) {
        //do nothing
    }

    public void removeTableModelListener(TableModelListener l) {
        //do nothing
    }
}
